package com.controller;

import com.bean.Student;
import com.bean.Teacher;
import com.bean.User;

import com.service.PaperService;
import com.service.UserService;
import com.utils.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;

/**
 * Created by Ming on 2016/9/9.
 */
@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    SubMenuController subMenuController;
    @Autowired
    UserService userService;
    @Autowired
    PaperService paperService;

    @RequestMapping("showUser.do")
    public String showUser(HttpServletRequest request) {
        String id = request.getParameter("id");
        User u = new User();
        u.setId(Long.parseLong(id));
        User user = userService.getUser(u);
        request.setAttribute("user", user);
        return "JSP/index/index";
    }

    @RequestMapping("insertUesr")
    public String insertUser(HttpServletRequest request){
        String userName = request.getParameter("userName");
        String userPassword = request.getParameter("userPassword");
        String uname = request.getParameter("uname");
        String userSex = request.getParameter("userSex");
        String statusName = request.getParameter("statusName");
        String mail = request.getParameter("mail");
        String userLocation = request.getParameter("userLocation");
        //获取当前时间
        Calendar c=Calendar.getInstance();
        int y=c.get(Calendar.YEAR);
        int m=c.get(Calendar.MONTH);
        int d=c.get(Calendar.DATE);
        String time=y+"-"+ m +"-"+d;
//        Date date=java.sql.Date.valueOf(time);

        //从web获取到的值存入bean
        if(statusName.equals("学生")){
            Student user = new Student();
            user.setUserName(userName);
            user.setUserPassword(MD5Util.string2MD5(userPassword));
            user.setUname(uname);
            user.setUserSex(userSex);
            user.setStatusName(statusName);
            user.setMail(mail);
            user.setUserLocation(userLocation);
            if(userService.getUsername(user)!=null){
                request.setAttribute("userNameError","此用户已注册");
                return "redirect:/submenu/member-add.do";

            }
            userService.insertUser(user);
            return "redirect:/submenu/member-list.do";

        }else if (statusName.equals("教师")){
            Teacher user = new Teacher();
            user.setUserName(userName);
            user.setUserPassword(MD5Util.string2MD5(userPassword));
            user.setUname(uname);
            user.setUserSex(userSex);
            user.setStatusName(statusName);
            user.setMail(mail);
            user.setUserLocation(userLocation);
            if(userService.getUsername(user)!=null){
                request.setAttribute("userNameError","此用户已注册");
                return "redirect:/submenu/member-add.do";
            }
            userService.insertUser(user);
            return "redirect:/submenu/member-list.do";

        }else if(statusName.equals("管理员")){
            User user = new User();
            user.setUserName(userName);
            user.setUserPassword(MD5Util.string2MD5(userPassword));
            user.setUname(uname);
            user.setUserSex(userSex);
            user.setStatusName(statusName);
            user.setMail(mail);
            user.setUserLocation(userLocation);
            if(userService.getUsername(user)!=null){
                request.setAttribute("userNameError","此用户已注册");
                return "redirect:/submenu/member-add.do";
            }
            userService.insertUser(user);
            return "redirect:/submenu/member-list.do";

        }

//        user.setCreateTime(date);
        return null;
    }

@RequestMapping(value = "delUserByUserName")
    public String delUser(HttpServletRequest request){
        String userName = request.getParameter("userName");
        userService.delUserByUserName(userName);
        userService.delStudentByUserName(userName);
        userService.delTeacherByUserName(userName);
        return "redirect:/submenu/member-list.do";

}

    @RequestMapping(value = "updateUserByUserName")
    public String updateUserByUserName(HttpServletRequest request){
        String userName = request.getParameter("userName");
        String uname = request.getParameter("uname");
        String userSex = request.getParameter("userSex");
        String statusName = request.getParameter("statusName");
        String mail = request.getParameter("mail");
        String userLocation = request.getParameter("userLocation");
        //从web获取到的值存入bean
        if (statusName.equals("学生")){
            Student student = new Student();
            student.setUserName(userName);
            student.setUname(uname);
            student.setUserSex(userSex);
            student.setStatusName(statusName);
            student.setMail(mail);
            student.setUserLocation(userLocation);
            userService.updateStudentByUserName(student);
        }else if (statusName.equals("教师")){
            Teacher teacher = new Teacher();
            teacher.setUserName(userName);
            teacher.setUname(uname);
            teacher.setUserSex(userSex);
            teacher.setStatusName(statusName);
            teacher.setMail(mail);
            teacher.setUserLocation(userLocation);
            userService.updateTeacherByUserName(teacher);
        }else if (statusName.equals("管理员")){
            User user = new User();
            user.setUserName(userName);
            user.setUname(uname);
            user.setUserSex(userSex);
            user.setStatusName(statusName);
            user.setMail(mail);
            user.setUserLocation(userLocation);
            userService.updateUserByUserName(user);
        }
        return "redirect:/submenu/member-list.do";

    }
}
